<?php
namespace app\web\home;

use app\index\controller\Home;
use app\lib\model\Token;
use app\web\model\Order;
use app\lib\exception\TokenException;
use app\web\model\User;
use app\web\model\Config;
use app\web\model\Technician;
use app\web\model\Shop;
use think\facade\Request;

/**
* 
*/
class Base extends Home
{
	public $uid = 0;
    public $user = null;
    public $auth_status = 0;    //商户端权限 0超级管理员 1普通管理员 2接单员
	public function initialize()
	{
		parent::initialize();
        $token = Request::instance()->header('token');
        if(!$token) throw new TokenException();
        $this->user =  User::where('token',$token)->find();
        if(!$this->user){
            throw new TokenException();
        }
        $this->appkey = Config::getAppKey();
        if($this->user['status'] == 1 && $this->appkey == 0){
            throw new TokenException(['code'=>700,'msg'=>'您已被拉黑']);
        }
        $this->uid = $this->user['id'];
        if($this->appkey == 1 && Technician::where('user_id',$this->uid)->value('is_period') == 1){
            throw new TokenException(['code'=>700,'msg'=>'您已到期']);
        }
        if($this->appkey == 2 && Shop::where('user_id',$this->uid)->value('is_period') == 1){
            throw new TokenException(['code'=>700,'msg'=>'您已到期']);
        }
        if($this->appkey == 2){  //商户端
            $this->setShop();
        }
        //客户端 0 小程序 1 h5
        $this->client = Config::getClient();
	}


    protected function getOrderNo(){
        $snowid = new \Godruoyi\Snowflake\Snowflake;
        $snowid->setStartTimeStamp(strtotime(date("2020-10-01")) * 1000);
        return date("Y") . $snowid->id();
    }

    protected function getCurrentId()
    {
        if($this->appkey == 0) return $this->uid;
        if($this->appkey == 1){
            $t_id = Technician::where('user_id',$this->uid)->value('id');
            if(!$t_id){
                throw new TokenException();
            }
            return $t_id;
        }elseif($this->appkey == 2){
            $shop = Shop::where('user_id',$this->uid)->field('id,p_status,p_id,auth_status')->find();
            if(!$shop){
                throw new TokenException();
            }
            return ($shop['p_id'] > 0 && $shop['p_status'] == 1) ? $shop['p_id'] : $shop['id'];
        }
    }

    protected function setShop(){
        $shop = Shop::where('user_id',$this->uid)->field('id,p_status,p_id,auth_status')->find();
        if(!$shop){
            throw new TokenException();
        }
        $this->auth_status = ($shop['auth_status'] > 0 && $shop['p_status'] == 1) ? $shop->auth_status : 0;
    }

    /**
     * 验证数据  重新验证方法
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @param  mixed        $callback 回调方法（闭包）
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
    {
        if (is_array($validate)) {
            $v = $this->app->validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                list($validate, $scene) = explode('.', $validate);
            }

            $v = $this->app->validate($validate);

            if (!empty($scene)) {
                $v->scene($scene);
            }
        }
        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        if (is_array($message)) {
            $v->message($message);
        }

        if ($callback && is_callable($callback)) {
            call_user_func_array($callback, [$v, &$data]);
        }
        if (!$v->check($data)) {
            if ($this->failException) {
                throw new TokenException(['code'=>400,'msg'=>$v->getError()]);
            }
            return $v->getError();
        }

        return true;
    }
    
    
}